<!-- eslint-disable vue/valid-attribute-name -->
<template>
  <div v-show="scene == 1">
    <el-card>
      <el-form :inline="true" class="form">
        <el-form-item label="餐厅名称:">
          <el-input
            placeholder="请你输入搜索餐厅名称"
            v-model="keyword"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            size="default"
            @click="search"
            icon="Search"
          >
            搜索
          </el-button>
          <el-button type="default" size="default" @click="reset">
            重置
          </el-button>
        </el-form-item>

        <el-form-item>
          <localSelect ref="local" @searchLocal="searchLocal" />
          <el-button
            style="margin-left: 10px"
            type="primary"
            size="default"
            @click="search"
            icon="Search"
          >
            搜索
          </el-button>
          <el-button type="default" size="default" @click="reset">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card style="margin: 10px 0px">
      <el-button type="primary" size="default" @click="addFood" icon="Plus">
        添加餐厅
      </el-button>
      <!-- table展示用户信息 -->
      <el-table style="margin: 10px 0px" border :data="foodArr">
        <el-table-column
          label="#"
          align="center"
          type="index"
        ></el-table-column>
        <el-table-column
          label="ID"
          width="50"
          align="center"
          prop="id"
        ></el-table-column>
        <el-table-column
          label="名称"
          align="center"
          prop="name"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          label="餐厅类型"
          width="100"
          align="center"
          prop="category"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          label="餐厅介绍"
          align="center"
          prop="introduction"
          min-width="200"
        ></el-table-column>
        <el-table-column
          label="平均价格（元）"
          align="center"
          width="100"
          prop="price"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          label="地址"
          align="center"
          prop="location"
        ></el-table-column>
        <el-table-column
          label="联系电话"
          align="center"
          prop="phone"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          label="评分"
          align="center"
          width=" 70"
          prop="rating"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column label="操作" align="center" width="300">
          <template #="{ row }">
            <el-popconfirm
              :title="`确定${
                row.is_online == 0 ? '审核通过' : '下架'
              }这条数据吗？`"
              width="400px"
              @confirm="isOnline(row)"
            >
              <template #reference>
                <el-button
                  :type="row.is_online == 0 ? 'primary' : 'info'"
                  :icon="row.is_online == 0 ? 'Top' : 'Bottom'"
                  size="small"
                ></el-button>
              </template>
            </el-popconfirm>
            <el-button
              type="warning"
              size="small"
              icon="User"
              @click="updateFood(row)"
            >
              编辑
            </el-button>
            <el-popconfirm
              :title="`你确定要删除 ${row.name} 吗？`"
              @confirm="deleteFood(row.id)"
            >
              <template #reference>
                <el-button type="danger" size="small" icon="Delete">
                  删除
                </el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
        v-model:current-page="pageNo"
        v-model:page-size="pageSize"
        :page-sizes="[5, 7, 9, 11]"
        :background="true"
        @current-change="getFood"
        @size-change="handler"
        layout="prev, pager, next, jumper,->,sizes,total"
        :total="total"
      />
    </el-card>
  </div>

  <div v-show="scene == 2">
    <FoodForm @changeScene="changeScene" ref="foodF"></FoodForm>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, ref } from 'vue'

import { ElMessage } from 'element-plus'
import useLayOutSettingStore from '@/store/modules/settings'
import { reqAllFood, reqDeleteFood, reqUpdateIsOnline } from '@/api/front/food'
import type { Records } from '@/api/front/food/type'
import localSelect from '@/views/front/localSelect.vue'
import FoodForm from './foodForm.vue'

let pageNo = ref<number>(1)
let pageSize = ref<number>(5)
let total = ref<number>(0)

// 美食数据
let foodArr = ref<Records>([])

// 切换表单
const scene = ref<number>(1)
// 表单组件
const foodF = ref<any>()

//控制顶部全选复选框不确定的样式

// let formRef = ref<any>()

// 关键字
let keyword = ref<string>('')
// 地址模糊查询
let location = ref<string>('')
// 获取模板setting仓库
let settingStore = useLayOutSettingStore()
onMounted(() => {
  getFood()
})

// 获取全部美食信息
const getFood = async (pager = 1) => {
  pageNo.value = pager
  let result: any = await reqAllFood(
    pageNo.value,
    pageSize.value,
    keyword.value,
    location.value,
  )
  if (result.code == 200) {
    foodArr.value = result.data
    total.value = result.total
  }
}
// 地址模糊查询
const searchLocal = (local: string) => {
  location.value = local
}
const handler = () => {
  getFood()
}
// 添加餐厅
const addFood = () => {
  scene.value = 2
  foodF.value.initAddFood()
}
// 切换组件回调
const changeScene = (obj: any) => {
  scene.value = obj.flag
  if (obj.params == 'update') {
    getFood(pageNo.value)
  } else {
    getFood()
  }
}
// 更新餐厅
const updateFood = (row: any) => {
  scene.value = 2
  foodF.value.initHasFood(row)
}
// 删除数据
const deleteFood = async (id: number) => {
  let result: any = await reqDeleteFood(id)
  if (result.code == 200) {
    ElMessage({
      type: 'success',
      message: '删除成功',
    })
    getFood(foodArr.value.length > 1 ? pageNo.value : pageNo.value - 1)
  } else {
    ElMessage({
      type: 'error',
      message: '删除失败',
    })
    getFood(pageNo.value)
  }
}
// 审核通过/下架
const isOnline = async (row: any) => {
  const isOnline = row.is_online == 0 ? 1 : 0
  const result: any = await reqUpdateIsOnline(row.id, isOnline)
  if (result.code == 200) {
    ElMessage({
      type: 'success',
      message: row.is_online == 0 ? '审核通过' : '下架',
    })
    getFood()
  } else {
    ElMessage({
      type: 'error',
      message: '操作失败',
    })
  }
}
// 搜索按钮回调
const search = () => {
  getFood()
  keyword.value = ''
  location.value = ''
}
// 重置按钮回调
const reset = () => {
  settingStore.refresh = !settingStore.refresh
}
</script>

<style lang="scss" scoped></style>
